﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;

namespace Gmantis.Controls
{
    public static class MessageBox
    {
        // Fields
        internal static Dictionary<MessageBoxButton, MessageBoxResult> defaultResult;
        internal const string LinkedResourcePath = "Resources/";

        // Methods
        static MessageBox()
        {
            Dictionary<MessageBoxButton, MessageBoxResult> dictionary = new Dictionary<MessageBoxButton, MessageBoxResult>();
            dictionary.Add(MessageBoxButton.OK, MessageBoxResult.OK);
            dictionary.Add(MessageBoxButton.OKCancel, MessageBoxResult.Cancel);
            dictionary.Add(MessageBoxButton.YesNo, MessageBoxResult.No);
            dictionary.Add(MessageBoxButton.YesNoCancel, MessageBoxResult.Cancel);
            defaultResult = dictionary;
        }

        public static void Show(string message)
        {
            Show(message, string.Empty, MessageBoxButton.OK, MessageBoxIcon.None, delegate(MessageBoxResult r)
            {
            });
        }

        public static void Show(string message, string caption)
        {
            Show(message, caption, MessageBoxButton.OK, MessageBoxIcon.None, delegate(MessageBoxResult r)
            {
            });
        }

        public static void Show(string message, Action<MessageBoxResult> callback)
        {
            Show(message, string.Empty, MessageBoxButton.OK, MessageBoxIcon.None, callback);
        }

        public static void Show(string message, string caption, MessageBoxButton button)
        {
            Show(message, caption, button, MessageBoxIcon.None, delegate(MessageBoxResult r)
            {
            });
        }

        public static void Show(string message, string caption, MessageBoxIcon icon)
        {
            Show(message, caption, MessageBoxButton.OK, icon, delegate(MessageBoxResult r)
            {
            });
        }

        public static void Show(string message, string caption, Action<MessageBoxResult> callback)
        {
            Show(message, caption, MessageBoxButton.OK, MessageBoxIcon.None, callback);
        }

        public static void Show(string message, string caption, MessageBoxButton button, MessageBoxIcon icon)
        {
            Show(message, caption, button, icon, delegate(MessageBoxResult r)
            {
            });
        }

        public static void Show(string message, string caption, MessageBoxButton button, Action<MessageBoxResult> callback)
        {
            Show(message, caption, button, MessageBoxIcon.None, callback);
        }

        public static void Show(string message, string caption, MessageBoxIcon icon, Action<MessageBoxResult> callback)
        {
            Show(message, caption, MessageBoxButton.OK, icon, callback);
        }

        public static void Show(string message, string caption, MessageBoxButton button, MessageBoxIcon icon, Action<MessageBoxResult> callback)
        {
            EventHandler handler = null;
            MessageBoxContent content = new MessageBoxContent
            {
                Message = message,
                Button = button,
                Icon = icon
            };
            if (ContentStyle != null)
            {
                content.Style = ContentStyle;
            }
            GWindow window = new GWindow
            {
                Header = caption,
                Content = content,
                IsResizable = false,
                ShowMinimizeButton = false
            };
            if (WindowStyle != null)
            {
                window.Style = WindowStyle;
            }
            window.CenterOnScreen();
            window.ShowModal();
            MessageBoxResult result = defaultResult[button];
            if (callback != null)
            {
                if (handler == null)
                {
                    handler = (s, e) => callback(result);
                }
                window.Closed += handler;
            }
            content.ButtonClick += delegate(MessageBoxResult r)
            {
                result = r;
                window.Close();
            };
            content.Loaded += (s, e) => content.Focus();
            content.KeyDown += delegate(object s, System.Windows.Input.KeyEventArgs e)
            {
                if (e.Key == Key.Escape)
                {
                    window.Close();
                }
            };
        }

        // Properties
        public static Style ContentStyle { get; set; }
        public static Style WindowStyle { get; set; }
    }
}
